//
//  HJTitleTextField.m
//  KaiChat
//
//  Created by lx on 2023/10/17.
//

#import "HJTitleTextField.h"


@interface HJTitleTextField()<QMUITextFieldDelegate>

@property (nonatomic, strong) UIImageView *iconImageView;
@property (nonatomic, weak) UIButton *eyeButton;
 

@end



@implementation HJTitleTextField


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        
        UILabel *titleLabel = [UILabel lableWithSuperView:self font:UIFontMediumMake(12) color:UIColor.tj_868391Color title:nil textAlignment:NSTextAlignmentLeft];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(0);
            make.left.offset(0);
        }];
        
        UIView *textFieldContentView = [UIView addToView:self];
        textFieldContentView.addCornerRadius(10);
        textFieldContentView.backgroundColor = UIColor.tj_F6F6F6Color;
        [textFieldContentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.inset(0);
            make.top.equalTo(titleLabel.mas_bottom).offset(10);
            make.height.offset(50);
        }];
        
        UIImageView *iconImageView = [UIImageView addToView:textFieldContentView];
        iconImageView.hidden = YES;
        [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(15);
            make.centerY.offset(0);
        }];
        
        QMUITextField *textField = [QMUITextField addToView:textFieldContentView];
        textField.font = UIFontMake(14);
        textField.delegate = self;
        textField.tintColor = UIColor.tj_cursorColor;
        textField.returnKeyType = UIReturnKeyDone;
        textField.textColor = UIColor.tj_1A1A1AColor;
        textField.placeholderColor = UIColor.tj_placeholderTextColor;
        [textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(15);
            make.right.inset(15);
            make.top.bottom.offset(0);
        }];
        
        UIButton *eyeButton = [UIButton addToView:textFieldContentView];
        eyeButton.hidden = YES;
        [eyeButton setImage:[UIImage imageNamed:@"eye"] forState:UIControlStateNormal];
        [eyeButton setImage:[UIImage imageNamed:@"eye_select"] forState:UIControlStateSelected];
        [eyeButton addTarget:self action:@selector(didClickEyeButton:) forControlEvents:UIControlEventTouchUpInside];
        [eyeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.inset(15);
            make.width.height.offset(20);
            make.centerY.offset(0);
        }];

        _titleLabel = titleLabel;
        _textField = textField;
        _iconImageView = iconImageView;
        _eyeButton = eyeButton;
    }
    return self;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

- (void)didClickEyeButton:(UIButton *)button {
    
    button.selected = !button.selected;
    
    if (button.selected) {
        _textField.secureTextEntry = NO;
    } else {
        _textField.secureTextEntry = YES;
    }
}

- (void)setSecureTextEntry:(BOOL)secureTextEntry {
    _secureTextEntry = secureTextEntry;
    
    _textField.secureTextEntry = secureTextEntry;

    if (secureTextEntry) {
        _eyeButton.hidden = NO;
        [_textField mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.inset(45);
        }];
    } else {
        _eyeButton.hidden = YES;
        [_textField mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.inset(0);
        }];
    }

}

- (void)setIconImage:(UIImage *)iconImage {
    _iconImage = iconImage;
    
    [_textField mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.offset(46);
    }];
    
    _iconImageView.hidden = NO;
    _iconImageView.image = iconImage;
}

@end
